﻿using System.Security.Cryptography;
using BizModels;
using BizModels.Common;
using DataModel;
using IRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Repositories
{
    public class BackLogRepository : RepositoryBase, IBackLogRepository
    {
        public BackLogRepository()
        {
            Context = new HinokyEntities();
        }

        public int AddBackLog(BackLogT backLogT)
        {
            if (backLogT == null) return 0;

            var backLog = new BackLog
            {
                State = backLogT.State,
                Category = backLogT.Category,
                Type = backLogT.Type,
                Importance = backLogT.Importance,
                Title = backLogT.Title,
                InsertDate = backLogT.InsertDate,
                UpdateDate = null,
                CreateUserKey = backLogT.CreateUser.UserKey,
                UpdateUserKey = null,
                ChargeUserKey = backLogT.CreateUser.UserKey
            };

            Context.BackLog.Add(backLog);
            Context.SaveChanges();

            return backLog.Idx;
        }

        public int SetBackLog(BackLogT backLogT)
        {
            var data = Context.BackLog.SingleOrDefault(s => s.Idx.Equals(backLogT.Idx));

            if (data == null) return 0;

            data.State = backLogT.State;
            data.Category = backLogT.Category;
            data.Type = backLogT.Type;
            data.Importance = backLogT.Importance;
            data.Title = backLogT.Title;
            data.UpdateDate = backLogT.UpdateDate;
            data.UpdateUserKey = backLogT.UpdateUser.UserKey;
            data.ChargeUserKey = backLogT.UpdateUser.UserKey;

            Context.SaveChanges();

            return data.Idx;
        }

        public List<BackLogT> GetBackLogList(BackLogSearchT backLogSearchT)
        {
            try
            {
                var query = GetBackLogQuery();

                var data = query.Skip((backLogSearchT.PageIndex - 1) * backLogSearchT.PageSize).Take(backLogSearchT.PageSize).ToList();

                return data;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }

        public BackLogT GetBackLog(int idx)
        {
            var query = GetBackLogQuery();

            var data = query.FirstOrDefault(s => s.Idx == idx);

            return data ?? new BackLogT();
        }

        public bool RemoveBackLog(List<int> idxList)
        {
            var data = Context.BackLog.Where(s => idxList.Contains(s.Idx)).ToList();

            Context.BackLog.RemoveRange(data);
            Context.SaveChanges();

            return true;
        }

        private IQueryable<BackLogT> GetBackLogQuery()
        {
            var query = from t1 in Context.BackLog
                        join t2 in Context.Members on t1.CreateUserKey equals t2.UserKey into list1
                        from l1 in list1.DefaultIfEmpty()
                        join t3 in Context.Members on t1.UpdateUserKey equals t3.UserKey into list2
                        from l2 in list2.DefaultIfEmpty()
                        join t4 in Context.Members on t1.ChargeUserKey equals t4.UserKey into list3
                        from l3 in list3.DefaultIfEmpty()
                        orderby t1.Idx descending 
                        select new BackLogT
                        {
                            Idx = t1.Idx,
                            State = t1.State,
                            Category = t1.Category,
                            Type = t1.Type,
                            Importance = t1.Importance,
                            Title = t1.Title,
                            InsertDate = t1.InsertDate,
                            UpdateDate = t1.UpdateDate,
                            CreateUser = new UserInfoT
                            {
                                UserId = l1.Id,
                                UserKey = l1.UserKey,
                                UserName = l1.Name
                            },
                            UpdateUser = l2 == null ? null : new UserInfoT
                            {
                                UserId = l2.Id,
                                UserKey = l2.UserKey,
                                UserName = l2.Name
                            },
                            ChargeUserKey = t1.ChargeUserKey
                        };

            return query;
        }
    }
}
